package itsm.isperp.control.form.process;

import itsm.isperp.control.HtmlControl;
import itsm.isperp.framework.utils.DateUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 * 进度控件
 * 
 * @author lizx
 * @date 2013-10-19
 * @since 1.0
 * 
 */
public class Processer implements HtmlControl {
	static final String STATE_READY = "ready";
	static final String STATE_WAIT = "wait";
	static final String STATE_DOING = "doing";
	static final String STATE_SINGULAR = "singular";

	private boolean showEmptyNode = false;
	private boolean end = false;

	private List<Node> nodes = new ArrayList<>();

	public boolean isEnd() {
		return end;
	}

	public void setEnd(boolean end) {
		this.end = end;
	}

	/**
	 * 设置某节点的时间值
	 * 
	 * @param name
	 *            节点名称
	 * @param date
	 *            事件
	 */
	public void addProcessNode(String name, Date date) {
		nodes.add(Node.newInstance(name, date));
	}

	/**
	 * 设置某节点的时间值
	 * 
	 * @param name
	 *            节点名称
	 * @param date
	 *            事件
	 */
	public void addProcessNode(String name, Date date, String tranName, String description, String comment) {
		nodes.add(Node.newInstance(name, date, tranName, description, comment));
	}

	public void addProcessNode(Node node) {
		nodes.add(node);
	}

	/**
	 * 输入html控件
	 * 
	 * @return html格式的控件
	 */
	public String renderHtml(boolean ifShowProceReady) {

		int size = nodes.size();

		int width = (size - 1) * 160;
		if (size == 0) {
			return null;
		}

		if (isEnd()) {
			width += 160;
		}

		StringBuilder sb = new StringBuilder("<div style='overflow-x:auto;overflow-y:hidden;position:relative'><div id='v_process' style='width:" + width + "px'>");

		String state, pState = null;

		for (int i = 0; i < size; i++) {
			// 如果最后时间有了，整个都已完成

			Node n = nodes.get(i);

			if (this.isEnd()) {
				state = STATE_READY;
			} else if (n.getDate() == null) {

				if (STATE_READY.equals(pState)) {
					state = STATE_WAIT;
				} else {
					if (showEmptyNode) {
						state = STATE_WAIT;

					} else {
						continue;
					}

				}
			} else {
				state = STATE_READY;
			}

			if (i > 0) {

				Node n2 = nodes.get(i - 1);
				if (STATE_READY.equals(pState)) {
					if (STATE_READY.equals(state)) {
						sb.append("<div class='proce ").append(STATE_READY).append("'>");

					} else {
						sb.append("<div class='proce ").append(STATE_DOING);
						sb.append("'>");
					}

				} else {
					sb.append("<div class='proce ").append(STATE_WAIT).append("'>");
				}

				if (StringUtils.isNotEmpty(n2.getTranName())) {
					if (STATE_READY.equals(state) && ifShowProceReady)
						sb.append(n2.getTranName());
				}
				sb.append("<ul>");
				sb.append("<li class='tx1'>&nbsp;</li>");
				sb.append("</ul>");
				sb.append("</div>");
			}

			sb.append("<div class='node fore ").append(state).append("'>");
			if (n.getComment() != null) {
				sb.append("<div  class='proce-comment' title='").append(n.getComment()).append("'>");
				sb.append(n.getComment());
				sb.append("</div>");
			}
			sb.append("<ul>");
			sb.append("<li class='tx1'>&nbsp;</li>");

			if (n.getDate() != null) {
				sb.append("<li class='tx2'>").append(n.getName());

				sb.append(StringUtils.isEmpty(n.getDescription()) ? "" : "<br>(" + n.getDescription() + ")");

				sb.append("</li>");
				sb.append("<li id='track_time_").append(i).append("' class='tx3'>");

				sb.append(DateUtils.formatDateTime(n.getDate()).replaceFirst(" ", "&nbsp;"));

				sb.append("</li>");
			} else {
				sb.append("<li class='tx2'>").append(n.getName());

				sb.append("</li>");
				sb.append("<li id='track_time_").append(i).append("' class='tx3'>");
				sb.append(StringUtils.isEmpty(n.getDescription()) ? "" : "(" + n.getDescription() + ")");

				sb.append("</li>");

			}

			sb.append("</ul>");
			sb.append("</div>");
			pState = state;

		}
		if (this.isEnd()) {

			Node n2 = nodes.get(size - 1);

			sb.append("<div class='proce ").append(STATE_READY).append("'>");

			if (StringUtils.isNotEmpty(n2.getTranName())) {
				if (ifShowProceReady)
					sb.append(n2.getTranName());
			}

			sb.append("<ul>");
			sb.append("<li class='tx1'>&nbsp;</li>");
			sb.append("</ul>");
			sb.append("</div>");
			sb.append("<div class='node fore ").append(STATE_SINGULAR).append("'>");

			sb.append("<ul>");
			sb.append("<li class='tx1'>&nbsp;</li>");
			sb.append("<li class='tx2'>").append("结束").append("</li>");

			sb.append("</ul>");
			sb.append("</div>");
		}

		sb.append("</div></div>");

		return sb.toString();

	}

	@Override
	public String renderHtml() {
		// TODO Auto-generated method stub
		return renderHtml(true);
	}
}
